package com.dao.impl;

import com.dao.TourDao;
import com.pojo.Tour;
import com.utils.WebUtils;

import java.util.List;

/**
 * @Author hjb
 * @Version 1.0
 * @Contact 1754729389@qq.com
 */
public class TourDaoImpl extends BaseDao implements TourDao {
    @Override
    public int addTour(Tour tour) {
        String sql = "insert into tourGroups(tour_name,tour_code,tour_maxNumber,tour_departureTime,tour_deadline,tour_destination,tour_basicPrice,tour_image)\n" +
                "values(?,?,?,?,?,?,?,?)";
        return update(sql,tour.getName(),tour.getCode(),tour.getMaxNumber(),tour.getDepartureTime(),tour.getDeadline(),tour.getDestination(),tour.getBasicPrice(),tour.getImage());
    }

    @Override
    public int delectTour(Integer id) {
        String sql = "delete from tourGroups where tour_id=?";
        return update(sql,id);
    }

    @Override
    public int updateTour(Tour tour) {
        String sql = "update tourGroups set tour_name=?,tour_code=?,tour_maxNumber=?,tour_departureTime=?,tour_deadline=?,tour_state=?,tour_destination=?,tour_basicPrice=?,tour_image=? where tour_id=?";
        return update(sql,tour.getName(),tour.getCode(),tour.getMaxNumber(),tour.getDepartureTime(),tour.getDeadline(),tour.getState(),tour.getDestination(),tour.getBasicPrice(),tour.getImage(),tour.getId());
    }

    @Override
    public Tour queryTour(Integer id) {
        String sql = "SELECT tour_id id,tour_name `name`,tour_code `code`,tour_maxNumber maxNumber,tour_departureTime departureTime,tour_deadline deadline,tour_state state,tour_destination destination,tour_basicPrice basicPrice,tour_image image\n" +
                "FROM tourGroups\n" +
                "where tour_id=?";
        return queryForOne(Tour.class,sql,id);
    }

    @Override
    public List<Tour> queryTours() {
        String sql = "SELECT tour_id id,tour_name `name`,tour_code `code`,tour_maxNumber maxNumber,tour_departureTime departureTime,tour_deadline deadline,tour_state state,tour_destination destination,tour_basicPrice basicPrice,tour_image image\n" +
                "FROM tourGroups";
        return queryForList(Tour.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from tourGroups";
        return Integer.parseInt(queryForSingleValue(sql).toString());
    }

    @Override
    public List<Tour> queryForPageItems(int beginNo, int pageSize) {
        String sql = "SELECT tour_id id,tour_name `name`,tour_code `code`,tour_maxNumber maxNumber,tour_departureTime departureTime,tour_deadline deadline,tour_state state,tour_destination destination,tour_basicPrice basicPrice,tour_image image\n" +
                "FROM tourGroups\n"
                +"limit ?,?";
        return queryForList(Tour.class,sql,beginNo,pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByPrice(int min, int max) {

        String sql = "SELECT COUNT(*)\n" +
                "FROM tourGroups\n" +
                "WHERE tour_basicPrice BETWEEN ? AND ?";

        return Integer.parseInt(queryForSingleValue(sql,min,max).toString());
    }

    @Override
    public List<Tour> queryForPageItemsByPrice(int beginNo, int pageSize, int min, int max) {
        String sql = "SELECT tour_id id,tour_name `name`,tour_code `code`,tour_maxNumber maxNumber,tour_departureTime departureTime,tour_deadline deadline,tour_state state,tour_destination destination,tour_basicPrice basicPrice,tour_image image\n" +
                "FROM tourGroups\n" +
                "WHERE tour_basicPrice BETWEEN ? AND ?\n" +
                "order by tour_basicPrice\n" +
                "LIMIT ?,?";

        return queryForList(Tour.class,sql,min,max,beginNo,pageSize);
    }
}
